package com.xianyun.book.command.query.strategy;

import com.alibaba.cola.exception.BizException;
import com.xianyun.book.common.core.handle.HandleParam;
import com.xianyun.book.common.core.handle.MultipleHandle;
import com.xianyun.book.common.core.handle.MultipleInvoker;
import com.xianyun.book.convertor.BookConvertor;
import com.xianyun.book.dto.clientobject.BookInfoCO;
import com.xianyun.book.gatewayimpl.database.BookMapper;
import com.xianyun.book.gatewayimpl.database.dataobject.BookDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author macos-zyj
 */
@Slf4j
@Component
public class BookMultipleHandle implements MultipleHandle<BookInfoCO,String> {

    @Resource
    private BookMapper bookMapper;

    @Override
    public List<BookInfoCO> doListQuery(HandleParam<String> handleParam, MultipleInvoker<BookInfoCO, String> next) {
        List<BookDO> resultList = selectByBookName(handleParam.getHandleData());
        if (null != resultList && resultList.size()>0){
            return BookConvertor.toDataTranslateList(resultList);
        }
        resultList = selectByAuthName(handleParam.getHandleData());
        if (null != resultList && resultList.size()>0){
            return BookConvertor.toDataTranslateList(resultList);
        }
        resultList = selectByTypeName(handleParam.getHandleData());
        if (null != resultList && resultList.size()>0){
            return BookConvertor.toDataTranslateList(resultList);
        }
        throw new BizException("BIZ_QUERY_NOT_FOUND","查询无结果！") ;
    }

    private List<BookDO> selectByBookName(String bookName){
        BookDO searchBook = new BookDO();
        searchBook.setBookName(bookName);
        return bookMapper.listBookBySort(searchBook);
    }

    private List<BookDO> selectByAuthName(String authName){
        BookDO searchBook = new BookDO();
        searchBook.setAuthor(authName);
        return bookMapper.listBookBySort(searchBook);
    }
    private List<BookDO> selectByTypeName(String typeName){
        BookDO searchBook = new BookDO();
        searchBook.setBookTypeName(typeName);
        return bookMapper.listBookBySort(searchBook);
    }
}
